<?php

/**
 * 管理用户模型
 * 
  + -----------------------------------------------------
 * Date 2013-3-8
  + -----------------------------------------------------
 */
class AdminModel extends CommonModel {

    protected $_validate = array(
        array('username', 'require', '请输入用户名！'),
        array('username', '/^[a-zA-Z\x{4e00}-\x{9fa5}]{3,20}$/u', '用户名由3-20位字母或中文组成！'),
        array('password', 'require', '请输入密码！', 0, 'regex', 1),
        array('email', 'email', '邮箱号码不正确！'),
    );
    protected $_auto = array(
        array('password', 'pwdHash', 1, 'callback'),
        array('password', 'autoPwd', 2, 'callback'),
        array('add_time', 'time', 1, 'function'),
        array('update_time', 'time', 2, 'function'),
    );

    protected function pwdHash() {
        if (isset($_POST['password'])) {
            return strtoupper(md5(strtoupper(md5(trim($_POST['password'])))));
        } else {
            return false;
        }
    }

    //修改信息时密码为空则不修改
    protected function autoPwd() {
        if (empty($_POST['password'])) {
            $admin = $this->where("id=$_POST[id]")->field('password')->find();
            if ($admin) {
                return $admin['password'];
            }
        } else {
            return strtoupper(md5(strtoupper(md5(trim($_POST['password'])))));
        }
    }

}

?>